<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *  
 * @package CanGoo
 * @author Mervin.Wong<mervin0502@163.com>
 * @copyright 
 * @license  http://www.apache.org/licenses/LICENSE-2.0
 * @version 0.1.0
 * @date 2013-2-28下午2:55:16
 *
 */
/**
 * CanGoo Post Class
 *
 * 调用文章内容，以在视图中使用
 *
 * @package		Blog
 * @subpackage	libraries
 * @category	libraries
 * @author		Mervin.Wong<mervin0502@163.com>
 * @link		http://mervin.me/project/cangoo
 */
 class Post {
 	/**
 	 * 超级对象的句柄
 	 */
 	private $_CI;
 	
 	/**
 	 * 获取要显示文章的所有ID；根据$current_page $per_page获取
 	 */ 
 	private $_id = array();
 	/**
 	 * 在获取的一组id中数组的索引号
 	 * 
 	 */
 	private $_current_index = 0;
 	 /**
 	 * 当前的一条记录数组
 	 */
 	private $_current_post;
 	/**
 	 * 当前文章的ID, 在获取单篇文章需要根据URL指定
 	 */
 	public $current_pid = 0;
	/**
	 * 当前的taxonomy的tid号
	 */
	 public $current_tid = 0;
 	/**
 	 * 当前页
 	 */
 	public $current_page = 1;
 	/**
 	 * 每页显示的文章数量
 	 */
 	public $per_page = 3;	
 	/**
 	 * 页面类型
 	 */
 	 public $content_type = 'index';
 	/**
 	 * 构造函数
 	 */
 	public function __construct(){
 		$this->_CI = & get_instance();
 		$this->_CI->load->model('blog/post_model');
 		$this->_CI->load->helper('url');
 		$this->_CI->load->helper('date');
 		$this->_CI->load->helper('common');
 	}
 	/**
 	 * 变量的设置与获取
 	 */
 	public function __get($name){
 		return $this->$name;
 	}
 	public function __set($name, $value){
 		$this->$name = $value;
 	}
 	/**
 	 * 初始化$this->_id
 	 * @access public
 	 * @return void description
 	 */
 	 public function run(){
 	 	$flag = true;
 	 	switch($this->content_type){
 	 		case 'index':
 	 			if(($this->_id = $this->_get_id()) != false){
 	 				//获取指定数量的文章ID
 	 				$flag = true;
 	 			}else{
 	 				$flag = false;
 	 			}
 	 			break;
 	 		case 'taxonomy':
 	 			if(($this->_id = $this->_get_id_by_taxonomy()) != false){
 	 				//获取指定数量且某一个分类/标签下的文章ID
 	 				$flag = true;
 	 			}else{
 	 				$flag = false;
 	 			}
 	 			break;
 	 		case 'article':
		 	 	if($this->_CI->post_model->is_have('blog_post', array('bpid'=>$this->current_pid))){
		 			$field = array('bpid', 'title', 'slug','uid', 'excerpt','addTime', 'viewNum', 'commentNum', );
					$this->_current_post = $this->_CI->post_model->get_one($field,'blog_post', array('bpid'=>$this->current_pid));
		 			$flag = true;
		 		}else{
		 			$flag = false;
		 		}
 	 			break;
 	 	}
 	 	return $flag;
 	 }
  	/**
 	 * 该篇文章是否存在
 	 */
 	public function have(){
 		if($this->_CI->post_model->is_have('blog_post', array('bpid'=>$this->current_pid))){
 			$field = array('bpid', 'title', 'slug','uid', 'excerpt','addTime', 'viewNum', 'commentNum', );
			$this->_current_post = $this->_CI->post_model->get_one($field,'blog_post', array('bpid'=>$this->current_pid));
 			return true;
 		}else{
 			return false;
 		}
 	}
 	
 	/**
 	 * 获取下一篇文章的ID
 	 */
 	public function next(){
 		$this->current_pid = current($this->_id);
 		if($this->current_pid){
 			next($this->_id);
 			
 			$field = array('bpid', 'title', 'slug','uid', 'excerpt','addTime', 'viewNum', 'commentNum', );
 			$this->_current_post = $this->_CI->post_model->get_one($field,'blog_post', array('bpid'=>$this->current_pid));
 			return true; 			
 		}else{
			return false; 		
 		}
 	}
 	
 	/************************************************************************
 	 * 
 	 * 文章内容信息
 	 * 
 	 *************************************************************************/
 	 /**
 	 * 获取文章标题
 	 */
 	public function title(){
		return $this->_current_post->title;
 	}
 	
 	/**
 	 * 获取文章的链接
 	 */
 	public function permalink(){
 		return site_url('blog/article/index/'.$this->current_pid);
 	}
 	/**
 	 * 获取作者
 	 */
 	public function author(){
		$uid = $this->_current_post->uid;
		$res = $this->_CI->post_model->get_one('name','user',array('uid'=>$uid));
		return $res->name;
 	}
 	/**
 	 * 获取日期信息
 	 */
 	public function date(){
 		return mdate('%Y-%m-%d %H:%i%s', $this->_current_post->addTime);
 	}
 	
 	/**
 	 * 获取文章的分类信息
 	 */
 	public function category(){
 		if($this->current_pid != 0){
 			$cat = $this->_CI->post_model->get_taxonomy_by_pid('category', $this->current_pid);
 			$return = '';
			foreach($cat as $v){
				$return  .= '<a href="'.site_url('blog/archive/category/'.$v['slug']).'" title="'.$v['name'].'">'.$v['name'].'</a>&nbsp;';
			}
 			return $return;
 		}else{
 			return false;
 		}
 	}
 	 	/**
 	 * 获取文章的标签信息
 	 */
 	 public function tag(){
 		if($this->current_pid != 0){
 			$tag = $this->_CI->post_model->get_taxonomy_by_pid('tag', $this->current_pid);
 			$return = '';
			foreach($tag as $v){
				$return  .= '<a href="'.site_url('blog/archive/tag/'.$v['slug']).'" title="'.$v['name'].'">'.$v['name'].'</a>&nbsp;';
			}
 			return $return;
 		}else{
 			return false;
 		} 	 	
 	 }
 	 
 	 /**
 	 * 获取文章的标签信息,只输出标签名
 	 */
 	 public function tag_name(){
 		if($this->current_pid != 0){
 			$tag = $this->_CI->post_model->get_taxonomy_by_pid('tag', $this->current_pid);
 			$return = '';
			foreach($tag as $v){
				$return  .= $v['name'].' ';
			}
 			return $return;
 		}else{
 			return false;
 		} 	 	
 	 }
 	 /**
 	  * 获取文章的浏览统计量
 	  */
 	 public function view_count(){
 		return $this->_current_post->viewNum;
 	 }
 	 
 	 /**
 	  * 获取文章的留言统计量
 	  */
 	 public function comment_count(){
 		return $this->_current_post->commentNum;
 	 }
 	 /**
 	  * 获取文章摘要
 	  */
 	 public function excerpt(){
 	 	if(empty($this->_current_post->excerpt)){
 	 		return cut_str($this->content(), 0, 300, 'UTF-8');
 	 	}else{
 			return $this->_current_post->excerpt;
 	 	}
 	 }
 	 /**
 	  * 获取文章内容 
 	  */
 	public function content(){
 		if($this->current_pid != 0){
 			$res =  $this->_CI->post_model->get_one('content', 'blog_post', array('bpid'=>$this->current_pid));
 			return $res->content;
 		}else{
 			return false;
 		}
 	}
 	/************************************************************************
 	 * 
 	 * 其他信息
 	 * 
 	 *************************************************************************/
 	/**
 	 * 输出头部信息（keywords description）
 	 */
	public function header(){
		switch($this->content_type){
			case 'index':
				$row = $this->_CI->post_model->get_one('value', 'options', array('name'=>'site_keywords', 'module'=>'blog'));
				$keywords = $row->value;
				$row = $this->_CI->post_model->get_one('value', 'options', array('name'=>'site_description', 'module'=>'blog'));
				$description = $row->value;
				break;
			case 'taxonomy':
				$row = $this->_CI->post_model->get_one(array('name','description'), 'taxonomy', array('tid'=>$this->current_tid));
				$keywords = $row->name;
				$description = $row->description;
				break;
			case 'article':
				$keywords = $this->tag_name();
				$description = $this->excerpt();
				break;
		}
		
		$head = '<meta name="keywords" content="'.$keywords.'" />';
		$head .= '<meta name="description" content="'.$description.'" />';
		$head .= '<meta name="generator" content="CanGoo 0.1" />';
		$head .= '<meta name="author" content="Mervin.Wong" />';
		$head .= '<meta name="copyright" content="2013" />';
		
		return $head;
	}
	
	public function taxonomy_name(){
		$row = $this->_CI->post_model->get_one('name', 'taxonomy', array('tid'=>$this->current_tid));
		return $row->name;
	}

	public function is_index(){
		if($this->content_type == 'index'){
			return true;
		}else{
			return false;
		}
	}
	
	public function is_taxonomy(){
		if($this->content_type == 'taxonomy'){
			return true;
		}else{
			return false;
		}		
	}
	
	public function is_article(){
		if($this->content_type == 'article'){
			return true;
		}else{
			return false;
		}		
	}
	 /**
 	 * 获取文章列表里的ID
 	 * @access private
 	 */
 	private function _get_id(){
 		$offset = ($this->current_page-1)*$this->per_page;
 		$result =  $this->_CI->post_model->get_pid($offset, $this->per_page);
 		if($result != false){
			$data = array();
			foreach($result as $v){
				array_push($data, $v['bpid']);
			}
			return $data;
 		}else{
 			return false;
 		}
 	}
 	
 	private function _get_id_by_taxonomy(){
 		$offset = ($this->current_page-1)*$this->per_page;
 		$result = $this->_CI->post_model->get_pid_by_taxonomy($this->current_tid, $offset, $this->per_page);
 		if($result != false){
			$data = array();
			foreach($result as $v){
				array_push($data, $v['bpid']);
			}
			return $data;
 		}else{
 			return false;
 		}
 	}
 }
 /* End of file Option.php*/
 /* Location:./apps/blog/libraries/Option.php*/